std::is_array

From Cppreference

Jump to: navigation, search
Defined in header <type_traits>

template< class T >
struct is_array;
(C++11 feature)

If T is an array type, provides the member constant value equal true. For any other type, value is false.

Contents

Inherited from std::true_type or std::false_type

Member objects

value true if T is an array type, false otherwise
(public member object)

[edit] Equivalent definition

[edit] Example

the following example uses is_array to differentiate between an array and a pointer

#include <iostream>
#include <type_traits>
 
template<typename A>
void describe(const A&,
              typename std::enable_if<std::is_array<A>::value>::type* = 0)
{
    std::cout << "A " << std::rank<A>::value << "D array\n";
}
 
template<typename A>
void describe(const A&,
              typename std::enable_if<std::is_pointer<A>::value>::type* = 0)
{
    std::cout << "A pointer\n";
}
 
int main()
{
    char s1[] = "test1";
    describe(s1);
    const char* s2 = "test2";
    describe(s2);
//  describe(s1[0]); // compile-time error
}

Output:

A 1D array
A pointer

[edit] See also

rank (C++11)
obtains the number of dimensions of an array type
(class template)
extent (C++11)
obtains the size of an array type along a specified dimension
(class template)
remove_extent (C++11)
removes one extent from the given array type
(class template)
remove_all_extents (C++11)
removes all extents from the given array type
(class template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages